package com.gin.kotlin.no_gay_alliance.model

import android.os.Parcelable
import com.gin.kotlin.no_gay_alliance.app.App.Companion.DOWNLOAD_DIR
import com.gin.kotlin.no_gay_alliance.base.DownloadUtils
import com.gin.kotlin.no_gay_alliance.enums.RetweetType
import com.gin.nga.enums.ImageThumb
import com.gin.nga.response.common.Attachment
import com.gin.nga.utils.IAttach
import kotlinx.parcelize.Parcelize

/**
 * 已上传附件数据
 * @property relativePath 相对地址
 * @property thumbBit 缩略图bit
 * @property filename 文件名，用于删除
 * @property originalFilename 原文件名
 * @property extension 后缀
 * @property source 来源标注信息
 * @property description 描述
 * @property size 附件大小，单位：KB
 * @property isChecked 是否被选中，用于附件列表删除和下载操作
 * @constructor
 */
@Parcelize
data class AttachmentModel(
    override var relativePath: String? = null,
    override var thumbBit: Int? = null,
    var filename: String? = null,
    var description: String? = null,
    var originalFilename: String? = null,
    var extension: String? = null,
    var source: String? = null,
    var type: String? = null,
    var size: Long? = null,
    var isImage: Boolean,
    var isChecked: Boolean = false,
) : Parcelable, IAttach {
    constructor(attachment: Attachment) : this(
        relativePath = attachment.relativePath,
        description = attachment.description,
        thumbBit = attachment.thumbBit,
        type = attachment.type,
        size = attachment.size,
        filename = attachment.filename,
        originalFilename = attachment.originalFilename,
        extension = attachment.extension,
        isImage = attachment.isImage(),
        source = RetweetType.find(attachment.originalFilename ?: ""),
    )

    fun obtainDownloadOption() = DownloadUtils.DownloadOption(
        url = getUrl(ImageThumb.ORIGINAL)!!,
        relativePath = originalFilename?.let { DOWNLOAD_DIR + it } ?: filename?.let { DOWNLOAD_DIR + it }!!
    )

}
